package kr.co.boogui.actions.admin.news;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.boogui.actions.ActionHandler;
import kr.co.boogui.dao.NewsDAO;
import kr.co.boogui.service.AdminService;

public class CheckNewsAction implements ActionHandler {
	@Override
	public String execute(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		
		// 페이징 변수선언
		int cpage = 1, perpage = 9, startno = 0;
		int  newstype=0;
		if(req.getParameter("newstype")!=null){
			newstype=Integer.parseInt(req.getParameter("newstype")); 
		}
		
		// 검색관련 변수
		String ftype = "", fkey = "";

		// cpage, startno 계산
		if (req.getParameter("cpage") != null) {
			cpage = Integer.parseInt(req.getParameter("cpage"));
			startno = (cpage - 1) * perpage;
		}

		// 총페이지수 계산
		int total = NewsDAO.totalNews(ftype, fkey,newstype);
		int allpage = (int) Math.ceil((float) total / perpage);
		// 네비게이션 시작번호 계산
		int startnav = 0;
		startnav = ((cpage - 1) / 10) * 10 + 1;
		// 글번호 계싼
		int startbno = 0;
		startbno = total - (perpage * (cpage - 1));

		// 검색유형과 검색값처리
		if (req.getParameter("findtype") != null) {
			ftype = req.getParameter("findtype");
		}
		if (req.getParameter("findkey") != null) {
			fkey = req.getParameter("findkey");
		}
		// 뷰로 전달할 데이터 지정

		// 게시판 관련 데이터

		String viewPage = "f|/WEB-INF/jsp/tiles/index.jsp";
		req.setAttribute("action", "../news/news_check.jsp");
		String method = req.getMethod();
		if (method.equalsIgnoreCase("get")){
			viewPage = AdminService.adminChk(req, "news_check");
			req.setAttribute("action" , "news_check.jsp");
			req.setAttribute("regi", "y");
			req.setAttribute("cpage", cpage);
			req.setAttribute("ftype", ftype);
			req.setAttribute("fkey", fkey);
			req.setAttribute("total", total);
			req.setAttribute("allpage", allpage);
			req.setAttribute("startnav", startnav);
			req.setAttribute("startbno", startbno);
			req.setAttribute("newstype", newstype);
			req.setAttribute("newslist", NewsDAO.All_listNews(startno, ftype, fkey, newstype));
		
		} else if (method.equalsIgnoreCase("post")) {		
			try {
		
			} catch (Exception e) {
				e.printStackTrace();
			}
				
		}		
				
		return viewPage;
	}
}
